﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Orid.Management.Statistics.Tools.Decomposition.Logic;

namespace  Orid.Management.Statistics.Tools.Decomposition.UI
 
{
    public partial class FormConfig : FormChildBase
    {
        private int modifyLine=-1;

        public FormConfig()
        {
            InitializeComponent();
            
        }

        private void FormConfig_Load(object sender, EventArgs e)
        {
            LoadSettingFileToListView();
            this.modifyToolStripMenuItem.Click += new EventHandler(modifyToolStripMenuItem_Click);
            this.buttonOK.Click += new EventHandler(buttonOK_Click);
        }

        void buttonOK_Click(object sender, EventArgs e)
        {
            SaveListItemToSettingFile();
        }

        void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ModifySelectItem();
        }


        private void ModifySelectItem()
        {
            if (listViewConfig.SelectedIndices.Count > 0)
            {

                GetModifyItem();
            }
            FormConfigChild child = new FormConfigChild();
            DialogResult result =   child.ShowDialog();
            if (result == DialogResult.OK)
            {
                RefushListViewSubItem();
            }

        }

        public void GetModifyItem()
        {
            modifyLine = listViewConfig.SelectedIndices[0];
             
                ConfigItem modifyItem = ConfigItem.GetInstance();
                if (modifyLine != -1)
                {
                   modifyItem.ItemName= listViewConfig.Items[modifyLine].SubItems[0].Text;
                   modifyItem.SubItem1 = listViewConfig.Items[modifyLine].SubItems[1].Text; 
                }
                 
           
            
        }

        private void RefushListViewSubItem()
        {
            ConfigItem modifyItem = ConfigItem.GetInstance();
            listViewConfig.Items[modifyLine].SubItems[0].Text = modifyItem.ItemName  ;
            listViewConfig.Items[modifyLine].SubItems[1].Text = modifyItem.SubItem1; 
        }

        private void SaveListItemToSettingFile()
        {
            foreach (ListViewItem item in listViewConfig.Items)
            {
                string property = item.SubItems[0].Text;
                string group = item.Group.Name;
                if (group == "Setting")
                {
                    Config.Setting.SettingInstance[property] = item.SubItems[1].Text;
                    

                }
                else
                {
                    Config.Setting.SettingInstance[property + "_" + group] = item.SubItems[1].Text;
                }

                               
            }

            //保存到xml文件
            Config.Setting.SettingInstance.SaveSettingToXmlFile();
             
        }
        private void LoadSettingFileToListView()
        {
           
            foreach (ListViewItem item in listViewConfig.Items)
            {
                string property =item.SubItems[0].Text;
                 string group = item.Group.Name;
                 if (group == "Setting")
                 {
                     if  (Config.Setting.SettingInstance[property]==null)
                     {
                         item.SubItems[1].Text = string.Empty;
                     }
                     else
                     {
                         item.SubItems[1].Text = Config.Setting.SettingInstance[property] ;
                     }
                     

                 }
                 else
                 {
                     item.SubItems[1].Text = Config.Setting.SettingInstance[property + "_" + group] ;
                    
                 }
               
            }
        }
    }
}
